<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="年份" prop="year">
        <el-date-picker clearable v-model="queryParams.year" type="year" placeholder="请选择年份" value-format="yyyy"></el-date-picker>
      </el-form-item>
      <el-form-item label="月份" prop="month">
        <el-select v-model="queryParams.month" placeholder="请选择车间" @change="workshopChange">
          <el-option v-for="item in monthList" :key="item.value" :label="item.name" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- <el-date-picker
          clearable
          v-model="queryParams.checkStartDate"
          type="date"
          value-format="yyyy-MM-dd 00:00:00"
          placeholder="请选择年份"
    ></el-date-picker>-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['mes:closeBill:add']">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="workstationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" align="center" width="50" />
      <el-table-column label="年份" align="center" prop="year"></el-table-column>
      <el-table-column label="月份" align="center" prop="month" />
      <!-- <el-table-column label="工位地点" align="center" prop="workstationAddress" /> -->
      <el-table-column label="关账时间" align="center" prop="closeTime" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:md:workstation:edit']"
          >修改</el-button>-->
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['mes:closeBill:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改工位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" :disabled="optType=='view'? true :false">
        <el-form-item label="关账时间" prop="closeTime">
          <el-date-picker clearable v-model="form.closeTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择关账时间"></el-date-picker>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  financialClosureList,
  financialClosureAdd,
  financialClosureDel,
  listWorkstation,
  getWorkstation,
  delWorkstation,
  addWorkstation,
  updateWorkstation
} from "@/api/mes/md/finance";

import { getLineByWorkshopId } from "@/api/mes/md/line";
export default {
  name: "Finance",
  dicts: ["sys_yes_no", "mes_workstation_type"],
  components: {},
  data() {
    return {
      monthList: [
        {
          name: "一月",
          value: "1"
        },
        {
          name: "二月",
          value: "2"
        },
        {
          name: "三月",
          value: "3"
        },
        {
          name: "四月",
          value: "4"
        },
        {
          name: "五月",
          value: "5"
        },
        {
          name: "六月",
          value: "6"
        },
        {
          name: "七月",
          value: "7"
        },
        {
          name: "八月",
          value: "8"
        },
        {
          name: "九月",
          value: "9"
        },
        {
          name: "十月",
          value: "10"
        },
        {
          name: "十一月",
          value: "11"
        },
        {
          name: "十二月",
          value: "12"
        }
      ],
      //自动生成编码
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        year: null,
        month: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        closeTime: [
          { required: true, message: "关账时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工位列表 */
    getList() {
      this.loading = true;
      financialClosureList(this.queryParams).then(response => {
        this.workstationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        workstationAddress: null,
        workshopId: null,
        workshopCode: null,
        workshopName: null,
        processId: null,
        processCode: null,
        processName: null,
        warehouseId: null,
        locationId: null,
        areaId: null,
        enableFlag: "Y",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.autoGenFlag = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        workstationCode: null,
        workstationName: null,
        workstationAddress: null,
        workshopId: null,
        workshopCode: null,
        workshopName: null,
        lineId: null,
        lineName: null,
        processId: null,
        processCode: null,
        processName: null,
        warehouseId: null,
        locationId: null,
        areaId: null,
        enableFlag: null
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workstationId);
      this.queryParams.mdWorkstationIdList = selection.map(
        item => item.workstationId
      );
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加财务关账";
      this.optType = "add";
    },
    // 查询明细按钮操作
    handleView(row) {
      this.reset();
      const workstationId = row.workstationId || this.ids;
      this.shopChange(workstationId);
      getWorkstation(workstationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看车间信息";
        this.optType = "view";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const workstationId = row.workstationId || this.ids;
      this.shopChange(row.workshopId);
      getWorkstation(workstationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工位";
        this.optType = "edit";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form, "======");
      this.$refs["form"].validate(valid => {
        if (valid) {
          // if (this.optType == "edit") {
          // updateWorkstation(this.form).then(response => {
          //   this.$modal.msgSuccess("修改成功");
          //   this.open = false;
          //   this.getList();
          // });
          // } else {
          financialClosureAdd(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
          // }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return financialClosureDel(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/md/workstation/export",
        {
          ...this.queryParams
        },
        `workstation_${new Date().getTime()}.xlsx`
      );
    },

    workshopChange(value) {
      this.$set(this.form, "lineId", "");
      console.log(this.form, "=======form1111====");
      this.shopChange(value);
    },
    lineChange(value) {
      this.$set(this.form, "lineId", value);
      console.log(this.form, "=======form====");
    },
    shopChange(value) {
      getLineByWorkshopId(value).then(res => {
        this.lineOptions = res.data;

        // this.loading = false;
      });
    }
  }
};
</script>
